<template>  
  <div class="pagination">
  <button @click="changepageno(pageNo-1)" :disabled="pageNo === 1">上一页</button>
  <button @click="changepageno(1)" v-show="StartEnd.start !== 1">1</button>
  <span v-show="StartEnd.start > 2">···</span>
  
  <!-- 连续分页 -->
  <button 
    @click="changepageno(index + StartEnd.start)" 
      :class="{active:pageNo === index + StartEnd.start}" 
      v-for="(n,index) in (StartEnd.end - StartEnd.start +1)" 
      :key="index"
  >
  {{ index + StartEnd.start}}
      <!-- {{ n + StartEnd.start -1 }} -->
  </button>
 
   <!-- 连续分页 -->
   
  <span  v-show="totalPage -1 > StartEnd.end"  >···</span>
  <button @click="changepageno(totalPage)"  v-show="StartEnd.end !== totalPage">{{ totalPage  }}</button>
  <button @click="changepageno(pageNo+1)" :disabled="pageNo === totalPage">下一页</button>
  
  <span>共 {{total}} 条</span>
</div>
</template>

<script>

export default {      
  name: "Pagination",    
  props:{
      total:{   //总数
          required:true,
          
      },
      pageSize:{   //每页展示数量化
          required:true,
          type:Number
      },
      pageNo:{   //当前页码
          required:true,
          type:Number
      },
      continues:{   //连续页数
          default:5,
          type:Number
      },
      changepageno:{
        type:Function,
        required:true
      }
  },
  computed:{
      //计算连续页码的 起始值 和 结束值
      StartEnd(){
          //解构
          let {pageSize,pageNo,continues,totalPage} = this;

          //  1. 创建 起始值  和 结束值
          let start;   
          let end;
          //2.计算
          //判断 当前的 continues >  totalPage
          if(continues > totalPage){
              start = 1;
              end = totalPage;
          }else{
                // 公式：  （连续页码-1）/2
              start = pageNo -  (continues-1)/2;
              end = pageNo + (continues-1)/2; 
              //当正常计算的start的值 小于1，statr = 1
              if(start < 1){
                  start = 1;
                  end = continues;
              }
              //当正常计算的end的值 大于 totalPage
              if(end > totalPage){
                  start = totalPage-continues +1;
                  end = totalPage;
              }



          }

          //返回
          return {start,end}
      },
      //计算当前的总页数
      totalPage(){
          return Math.ceil(this.total / this.pageSize);
      }
  }

};
</script>

<style lang="less" scoped>
.pagination {
  text-align: center;

  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: gray;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #c81623;
      color: #fff;
    }
  }

  span {
    display: inline-block;
    line-height: 28px;
    font-size: 14px;
    color: gray;
    vertical-align: middle;
  }
}
</style>